<template>
  <div class="search">
    <n-card :bordered="false" class="mt-4 proCard">
      <n-grid cols="1" :x-gap="12">
        <n-gi>
          <n-form
            :model="queryForm"
            ref="queryFormRef"
            label-placement="left"
            inline>
            <n-grid :x-gap="12"  :cols="4" responsive="screen" @keydown.enter.native="handleSearch">
              <n-gi>
                <n-form-item label="岗位名称" path="jobName">
                  <n-input placeholder="请输入岗位名称" v-model:value="queryForm.jobName" />
                </n-form-item>
              </n-gi>
              <n-gi>
                <n-form-item label="部门" path="deptId">
                  <n-tree-select :options="deptTreeData" v-model:value="queryForm.deptId"/>
                </n-form-item>
              </n-gi>
              <n-gi>
                <n-form-item style="margin-right: 0" class="br">
                  <n-space>
                    <n-button type="primary" @click="handleSearch">
                      <template #icon>
                        <n-icon>
                          <IosSearch/>
                        </n-icon>
                      </template>查询</n-button>
                    <n-button @click="handleReset">
                      重置</n-button>
                  </n-space>
                </n-form-item>
              </n-gi>
            </n-grid>
          </n-form>
          <n-grid :x-gap="12" :cols="2" class="operation" style="margin-bottom: 1vh" >
            <n-gi>
              <n-space>
                <n-button type="primary" @click="handleAdd">
                  <template #icon>
                    <n-icon>
                      <IosAdd/>
                    </n-icon>
                  </template>添加</n-button>
                <n-button type="error" @click="deleteAll">
                  <template #icon>
                    <n-icon>
                      <IosTrash/>
                    </n-icon>
                  </template>
                  删除</n-button>
              </n-space>
            </n-gi>
          </n-grid>
          <n-grid :cols="1" style="margin-bottom: 1vh;">
            <n-gi :span="1">
              <n-alert type="info" size="small">
                已选择
                <span class="select-count">{{selectedKeys.length}}</span> 项
                <a class="select-clear" @click="clearSelect" v-if="selectedKeys.length>0">清空</a>
              </n-alert>
            </n-gi>
          </n-grid>
          <n-grid :cols="1">
            <n-gi>
              <n-data-table
                ref="table"
                :columns="column"
                :pagination="pagination"
                :loading="loading"
                :data="data"
                :row-key="row => row.id"
                @update:checked-row-keys="handleCheck"
                @update:page-size="changePageSize"
                :checked-row-keys="selectedKeys"
              >
              </n-data-table>
            </n-gi>
          </n-grid>
        </n-gi>
      </n-grid>
    </n-card>
    <job-form ref="jobFormRef" @success="handleSuccess"/>
  </div>
</template>

<script lang="ts">
import {defineComponent,ref,h,onMounted} from 'vue'
import {useMessage,NButton,NTag,useDialog} from 'naive-ui'
import {IosSearch,IosAdd,IosTrash} from '@vicons/ionicons4'
import JobForm from "@/views/system/job/JobForm.vue";
import {findJobList,deleteJob} from "@/api/system/job";
import {getDeptTreeData} from "@/api/system/dept";
import {getDictByCode,getDictLabel} from "@/utils/dictUtil";

export default defineComponent({
  name: "Job",
  components:{
    IosSearch,
    IosAdd,
    IosTrash,
    NTag,
    JobForm
  },
  setup(){
    const message=useMessage();
    const dialog=useDialog();
    let loading=ref(false);
    let data=ref([]);
    let jobFormRef=ref(null);
    let deptTreeData=ref([]);
    let selectedKeys=ref([]);
    let initQueryForm=()=>{
      return  {
        page:1,
        pageSize:10,
        deptId:null,
      }
    }
    let queryForm=ref(initQueryForm());
    const column=ref([
      {
        type: "selection",
        width: 60,
        align: "center",
        fixed: "left"
      },
      {
        type: "index",
        width: 60,
        align: "center",
        fixed: "left"
      },
      {
        title:'岗位名称',
        key:'jobName',
        align: 'center',
      },
      {
        title: '所属部门',
        align: 'center',
        key: 'deptName',
      },
      {
        title: '排序',
        align: 'center',
        key: 'sort',
      },
      {
        title: '创建时间',
        align: 'center',
        key: 'createTime',
        sortable: true,
      },
      {
        title: '操作',
        key: 'actions',
        align:'center',
        render: function (row) {
          return h("div", [
            h(
              NButton,
              {
                type: "primary",
                size: "small",
                style: {
                  marginRight: "5px"
                },
                onClick: () => handleEdit(row)
              },
              {default: () => '编辑'}
            ),
            h(
              NButton,
              {
                type: "error",
                size: "small",
                onClick: () => handleDelete(row)
              },
              {default: () => '删除'}
            )
          ]);
        }
      }
    ])
    const pagination=ref({
      itemCount:0,
      prefix ({ itemCount }) {
        return `共 ${itemCount} 项`
      },
      page: 1,
      pageSize: 10,
      showSizePicker: true,
      pageSizes: [10, 20, 30,40,50],
    })
    function handleSearch(){
      queryJobList();
    }
    function handleReset(){
      queryForm.value=initQueryForm();
      queryJobList();
    }
    function handleAdd(){
      jobFormRef.value.title='添加岗位';
      jobFormRef.value.modalShowVisible=true;
      jobFormRef.value.loadDeptData();
    }
    function clearSelect() {
       selectedKeys.value=[];
    }
    function handleCheck(keys) {
      selectedKeys.value=keys;
    }
    function changePageSize(value) {
      queryForm.value.pageSize=value;
      pagination.value.pageSize=value;
      queryJobList();
    }
    function handleEdit(row){
      let record={};
      record=Object.assign(record,row);
      jobFormRef.value.jobForm=record;
      jobFormRef.value.title='编辑岗位';
      jobFormRef.value.modalShowVisible=true;
      jobFormRef.value.loadDeptData();
    }
    function handleDelete(row){
      selectedKeys.value.push(row.id);
      deleteAll();
    }
    function deleteAll() {
      if(selectedKeys.value.length<=0){
        message.warning('请选择您要删除的数据');
        return;
      }
      dialog.warning({
        title: '警告',
        content: '你确定要删除所选的'+selectedKeys.value.length+'条数据吗?删除后无法恢复',
        positiveText: '确定',
        negativeText: '取消',
        onPositiveClick:()=>{
          const ids = selectedKeys.value.join(",");
          deleteJob(ids).then(res=>{
            if(res){
              message.success('删除成功');
              queryJobList();
              selectedKeys.value=[]
            }else{
              message.error(res.message)
            }
          })
        },
        onNegativeClick: () => {
        }
      })
    }
    function handleSuccess() {
      queryJobList();
    }
    function queryJobList(){
      findJobList(queryForm.value).then(res=>{
        data.value=res.records;
        pagination.value.itemCount=res.total;
      })
    }
    onMounted(async()=>{
      getDeptTreeData().then(data=>{
        deptTreeData.value=data;
      })
      queryJobList();
    })
    return {
      queryForm,
      pagination,
      column,
      loading,
      data,
      deptTreeData,
      jobFormRef,
      selectedKeys,
      clearSelect,
      handleSearch,
      handleReset,
      handleAdd,
      deleteAll,
      handleCheck,
      changePageSize,
      handleSuccess
    }
  }
})
</script>

<style lang="less" scoped>
@import "../../../styles/Table.less";
</style>
